# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity_study_code
File Name: test.py
Author: kij13
Create Date: 2021/3/28
-------------------------------------------------
"""

# from multiprocessing import Process
# import time
#
# def foo():
#     print(123)
#     time.sleep(3)
#     print("end123")
#
#
# def bar():
#     print(456)
#     time.sleep(1)
#     print("end456")
#
#
# if __name__ == '__main__':
#     p1 = Process(target=foo, )
#     p2 = Process(target=bar, )
#
#     p1.daemon = True
#     p1.start()
#     p2.start()
#     print("main-------")

# from threading import Thread
# import time
#
# def f1():
#     print(111)
#     time.sleep(3)
#     print("任务1")
#
#
# def f2():
#     print(222)
#     time.sleep(1)
#     print("任务2")
#
#
# if __name__ == '__main__':
#     t1 = Thread(target=f1)
#     t2 = Thread(target=f2)
#
#     t1.daemon = True
#     t1.start()
#     t2.start()
#     print("main-------")



# from threading import Thread, Lock
# import time
#
# n = 10
#
# def task(mutex):
#     global n
#     mutex.acquire()
#     temp = n
#     time.sleep(0.1)
#     n = temp - 1
#     mutex.release()
#     # n-=1
#     print(n,'inner task')
#
# if __name__ == '__main__':
#     mutex = Lock()
#     t_l = []
#     for i in range(10):
#         t = Thread(target=task,args=(mutex,))
#         t_l.append(t)
#         t.start()
#
#     # for t in t_l:
#     #     t.join()
#
#     print('主', n,'123456789')
#
#     for t in t_l:
#         t.join()
#
#     print('主', n,'------')





# from multiprocessing import Process, Lock
# import time
#
# n = 10
#
# def task(mutex):
#     global n
#     # mutex.acquire()
#     # temp = n
#     # time.sleep(0.1)
#     # n = temp - 1
#     # mutex.release()
#     n-=1
#     print(n,'inner task')
#
# if __name__ == '__main__':
#     mutex = Lock()
#     p_l = []
#     for i in range(10):
#         p = Process(target=task,args=(mutex,))
#         p_l.append(p)
#         p.start()
#
#     for p in p_l:
#         p.join()
#
#     print('主', n)


# from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# import requests
# import os
#
#
# def get_page(url):
#     print('<进程%s> get %s' % (os.getpid(), url))
#     respone = requests.get(url)
#     if respone.status_code == 200:
#         return {'url': url, 'text': respone.text}
#
#
# def parse_page(res):
#     res = res.result()
#     print('<进程%s> parse %s' % (os.getpid(), res['url']))
#     parse_res = 'url:<%s> size:[%s]\n' % (res['url'], len(res['text']))
#     with open('db.txt', 'a') as f:
#         f.write(parse_res)
#
#
#
# if __name__ == '__main__':
#     urls = [
#         'https://www.baidu.com',
#         'https://www.python.org',
#         'https://www.openstack.org',
#         'https://help.github.com/',
#         'http://www.sina.com.cn/'
#     ]
#
#     p = ProcessPoolExecutor(3)
#     for url in urls:
#         p.submit(get_page, url).add_done_callback(parse_page)  # parse_page拿到的是一个future对象obj，需要用obj.result()拿到结果



##同步
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import os, time, random


def task(n):
    print('task 任务 %s ' % n)
    time.sleep(random.randint(1, 3))
    return n ** 2

def end(res):

    print(res.result())


if __name__ == '__main__':
    pool = ProcessPoolExecutor(max_workers=2)
    for i in range(4):
        future = pool.submit(task, i)
        # end(future)
        future.add_done_callback(end)
        # print(future.result())   就是end(future)